package Server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

class SThread extends Thread {

    String LoginName;
    private BufferedReader In;
    private PrintWriter Out;
    private Socket socket;
    String bookname = "English Book ", bookid = "1";
    int bookamount = 200, bookprice = 10;

    public SThread(Socket sock) {
        socket = sock;
    }

    @Override
    public void run() {
        try {
            In = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Out = new PrintWriter(socket.getOutputStream());
            while (true) {
                int Choose = Integer.parseInt((In.readLine()));
                if (Choose == 1) {
                    Userlogin();
                } else {
                    BuyBook();
                }

            }
        } catch (IOException e) {
            System.out.println("disconnect");
        }

    }

    public void Userlogin() throws IOException {
        String[] szUserData = new String[3];
        boolean send = false;
        String name = In.readLine();
        String password = In.readLine();
        String line;
        InputStream is = new FileInputStream(".\\users.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while ((line = reader.readLine()) != null) {
            szUserData = line.split(";");
            if (szUserData[0].equals(name) && szUserData[1].equals(password)) {
                Out.println("Welcome you " + szUserData[0]);
                Out.println(szUserData[2]);
                Out.println("LoginOK");
                Out.flush();
                LoginName = szUserData[0];
                send = true;
                break;
            } else {
            }

        }
        if (!send) {
            Out.println("notlogin");
            Out.flush();
            is.close();
        } else {
            is.close();
        }
    }

    public void BuyBook() throws IOException {
        Out.println(bookid + "  " + bookname + "there are" + bookamount + " At" + bookprice);
        Out.println("sender");
        Out.flush();
        int uamount = Integer.parseInt(In.readLine());
        int uprice = Integer.parseInt(In.readLine());
        if (uamount <= bookamount) {
            if (uprice >= bookprice) {
                Out.println("BidOK");
                Out.flush();
                String TotleFileLin;
                String szFileInfo = null;
                BufferedReader inputFromFile = new BufferedReader(new FileReader(".\\offer.txt"));
                while ((TotleFileLin = inputFromFile.readLine()) != null) {
                    if (szFileInfo != null) {
                        szFileInfo += TotleFileLin + "\n";
                    } else {
                        szFileInfo = TotleFileLin + "\n";
                    }
                }
                inputFromFile.close();
                if (szFileInfo != null) {
                    szFileInfo += LoginName + ";" + uamount + ";" + uprice;
                } else {
                    szFileInfo = LoginName + ";" + uamount + ";" + uprice;
                }
                BufferedWriter outputTofile = new BufferedWriter(new FileWriter(".\\offer.txt"));
                outputTofile.write(szFileInfo);
                outputTofile.close();
            } else {
                Out.println("not");
            }
        }

    }
}
